<?php

namespace IDock\Insurance\AICS;

use IDock\Common\BaseInterface\BuilderInterface;
use IDock\Common\Tools\SoapXml;
use IDock\Common\Tools\XMLTools;
use IDock\Http\Request;
use IDock\Http\Response;
use IDock\Insurance\AICS\Request\AllAICSRequest;
use IDock\Insurance\AICS\Response\AllAICSResponse;

class Builder implements BuilderInterface
{
    protected $ip;

    protected $config;

    protected $userName;

    protected $password;

    protected $PCPayUrl;

    protected $WAPPayUrl;

    protected $gateway;

    public function getConfig(): array
    {
        return $this->config;
    }

    public function execute(Request $request,Response $multiplexResponse = null): Response
    {
        /*** @var AllAICSRequest $request */
        $request->loadConfig($this->config);

        $header = [
            'Content-Type' => 'text/xml; charset=utf-8',
        ];
        $request->setHeaders($header);
        $requestId = sha1(uniqid('', true));
        $request->setRequestId($requestId);
        $request->setGateway($this->gateway.$request->code.'?wsdl');
        if(property_exists($request,'userName')){
            $request->userName = $this->userName;
        }
        $param = [
            'authorityData' => [
                'userName' => $this->userName,
                'password' => $this->password,
            ],
        ];
        $param = array_merge($param, $this->generate($request));
        $request->setPreParameter($param);

        $this->buildBiz($request);
        return $this->request($request,$multiplexResponse);
    }

    public function generate(Request $request): array
    {
        return $request->generate();
    }

    public function sign(Request $request): Request
    {
        return $request;
    }

    public function buildBiz(Request $request)
    {
        $bizParam = SoapXml::ArrayToXml($request->getPreParameter(), $request->envelopeName);
        if($request->code == 'newImageUpService'){
            $bizParam = SoapXml::ArrayToXml($request->getPreParameter(), $request->envelopeName,'newImgUpLoadInfoRequest');
        }
        if(in_array($request->code,['newGetPayNoService','DzbdWebService','NewQueryYwCvrgWebService'])){
            $bizParam = SoapXml::ArrayToXml($request->getPreParameter(), $request->envelopeName,'request','tns');
        }
        if($request->code == 'payCallback'){
            $param = $request->getPreParameter();
            unset($param['authorityData']);
            $bizParam = SoapXml::ArrayToXml($request->getPreParameter(), $request->envelopeName,'');
        }
        $request->setBizParam($bizParam);
        return $request;
    }

    public function exceptionResponse($request, $errorMessage): Response
    {
        $response = $request->getResponse();

        $response = new $response;
        $response->setCode('500');
        $response->setError($errorMessage);

        return $response;
    }

    public function request(Request $request,Response $multiplexResponse = null): Response
    {
        $response = $request->request($request, $multiplexResponse);
        $response->setVisitor($request->getVisitor());
        if($response->isSuccess()) {
            $response->printerInit();
        }
        return $response;
    }

    public function encrypt(Request $request)
    {
        // TODO: Implement encrypt() method.
    }
}


